# -*- coding: utf-8 -*-
import requests

headers = {
    'Connection': 'close',
    'Authorization': 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyYjJmOGMyYTdmZmM4NDIyOThjNzViYmNjMDczZTgwMyIsInBmIjoiTU9CSUxFIiwidWQiOiJiMTdkZjU5ZDg5OTc0MjFlOGMzYTEzZWQzZTc3YTE5ZSIsInV0IjoxLCJ0aSI6MCwiZXQiOjI1OTIwMDAwMDB9.4pStWM9Ih6drQGcfMlTq3BgAKv--kqr8MTmSEiQmeEcjXV2TrAIck_YcNHX_EkAq3TJyAaXlVNKoen-72WR18A',
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1'
}


def get_video_job(api_url):
    # global response
    url = api_url
    response = None
    try:
        response = requests.post(url, headers=headers).json()
    except Exception as e:
        print('获取任务失败', e)
    if response is None:
        return False, None, None, None

    data = response.get('data', None)
    if data is None:
        return False, None, None, None
    content = data.get('content')
    job_id = data.get('job_id') if len(data.get('job_id')) > 0 else None
    if content is None:
        return False, None, None, None
    return True, data, content, job_id

#def save_job_api(api_url, job_id, consume_second, mark, worker_ip, oss_url, status):
def save_job_api(api_url, job_id, worker_ip, consume_seconds=0, mark='', oss_url='', status='pending', video_time=0, is_new=True, expire_time='',cover=''):
    url = api_url
    data = {
        "job_id": job_id,
        "consume_second": consume_seconds,
        "mark": mark,
        "worker_name": worker_ip,
        "oss_url": oss_url,
        "status": status,
        "video_time": video_time,
        "is_new": is_new,
        "expire_time": expire_time,
        "cover": cover
    }
    try:
        response = requests.post(url, headers=headers, json=data)
    except Exception as e:
        print('视频任务保存失败', e)


def save_video_detail(api_url, job_id, worker_ip,consume_seconds=0, embed_seconds=0,audio_seconds=0,video_seconds=0,merge_seconds=0,mark='',  status='in_progress'):
    url = api_url
    data = {
        "job_id": job_id,
        "consume_second": consume_seconds,
        "embed_second": embed_seconds,
        "audio_second": audio_seconds,
        "video_second": video_seconds,
        "merge_second": merge_seconds,
        "mark": mark,
        "worker_name": worker_ip,
        "status": status
    }
    try:
        response = requests.post(url, headers=headers, json=data)
    except Exception as e:
        print('视频详情保存失败', e)


def retry_add_job(api_url, datas):
    url = api_url
    try:
        response = requests.post(url, headers=headers, json=datas)
    except Exception as e:
        print('任务重新入库失败', e)


def push_message(api_url,user_id):
    url = api_url
    data = {
        "userId": user_id,
        "title": 'kyk-gpt-video',
        "body": '视频生成成功',
        "pageUrl": 'https://www.keyike.ltd/kesurong/kyk-gpt/video'
    }
    response = requests.post(url, headers=headers, data=data)
    result = response.json().get('msg')
    return result


# #
# url = 'http://192.168.1.51:8000/api/v1/job-producer'
# data = {'user_id': '2b2f8c2a7ffc842298c75bbcc073e803', 'job_id': '9a69128a89cf440bad52b9890d11b2ee', 'msg_id': '8', 'content': '金城银行金企贷是一款专注于企业税务融资的产品，最高可达500万的贷款额度，年化利率在5%至24%之间浮动，具体利率将根据您的信用评级和企业财务状况进行定价，以确保您获得最优惠的融资费用。贷款期限灵活多样，可选定12、24、36期，目前暂时仅开放24期。这一特色使得企业能够更好地根据自身经营情况规划还款计划，提高还款的灵活性。还款方式方面，金企贷提供了等额本金和随借随还两种选择。'}
# # data = {'user_id': '2b2f8c2a7ffc842298c75bbcc073e803', 'job_id': '9a69128a89cf440bad52b9890d11b2ee', 'content': '金城银行金企贷是一款专注于企业税务融资的产品，最高可达500万的贷款额度，年化利率在5%至24%之间浮动，具体利率将根据您的信用评级和企业财务状况进行定价，以确保您获得最优惠的融资费用。贷款期限灵活多样，可选定12、24、36期，目前暂时仅开放24期。这一特色使得企业能够更好地根据自身经营情况规划还款计划，提高还款的灵活性。还款方式方面，金企贷提供了等额本金和随借随还两种选择。'}
# retry_add_job(url, data)


# url = 'https://www.keyike.ltd/ksr-api/app/agent/device/na/push-message'
# user_id = '2b2f8c2a7ffc842298c75bbcc073e803'
# push_message(url, user_id)